import { get, post, message } from '@/utils'
import type { ProColumns } from '../../packages/pro-components/components/pro-table'
import GroupBtn from '@/components/common/GroupBtn.vue'
export interface MyProColumns extends ProColumns {
  btnList?: BtnItem[] | ((any) => BtnItem[])
  valueSearch?: boolean //是否开启远程搜索
  api?: string //请求地址
  filedName?: { label: string; value: string } //转换数据成option
}
const useTable = () => {
  /**
   *
   * @param columns
   * @param defaultOptions 列的默认配置项
   * @returns
   */
  const formatColumns = (columns: MyProColumns[], defaultOptions?: Partial<MyProColumns>) => {
    const defaultOption: MyProColumns = {
      align: 'center',
      ellipsis: false,
      tooltip: true,
      dataIndex: '',
      title: '',
      hideInSearch: true, //是否显示在搜索栏
      formItemProps: {
        'label-col-props': { span: 10 },
        'wrapper-col-props': { span: 14 },
      },
    }
    const processColumns = (columns: MyProColumns[]) => {
      const processedColumns: MyProColumns[] = []
      const processColumn = (column: MyProColumns) => {
        const processedColumn: MyProColumns = {
          ...defaultOption,
          ...defaultOptions,
          ...column,
          slotName: column.dataIndex,
        }
        if (Array.isArray(column.children)) {
          processedColumn.children = column.children.map(child => {
            const processedChild: MyProColumns = {
              ...defaultOption,
              ...defaultOptions,
              ...child,
              slotName: child.dataIndex,
            }
            return processColumn(processedChild)
          })
        }

        return processedColumn
      }
      for (const column of columns) {
        const processed = processColumn(column)
        /**处理自定义按钮列表 */
        if (Array.isArray(processed.btnList)) {
          processed.render = ({ record }) => h(GroupBtn, { record, list: processed.btnList as BtnItem[] })
        }
        // if (processed.valueSearch) {
        //   const _searchValueEnum: any = {}
        //   get({ url: processed.api as string }, ({ data }) => {
        //     data.forEach(element => {
        //       _searchValueEnum[element[processed.filedName.value]] = { text: element[processed.filedName?.label] }
        //     })
        //     processed.valueEnum = _searchValueEnum
        //   })
        // }
        processedColumns.push(processed)
      }

      return processedColumns
    }
    return processColumns(columns)
  }
  return { formatColumns }
}

export default useTable
